package com.liang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liang.mall.entity.Menu;
import com.liang.mall.myAnnotation.LogMethodInfo;
import com.liang.mapper.MenuMapper;
import com.liang.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author lin
 * @since 2021-05-07
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    MenuMapper menuMapper;


    @Override
    @LogMethodInfo(methodDesc = "查询菜单",operation = "select")
    public List<Menu> selectMenuAll() {
        List<Menu> menus = menuMapper.selectMenuAll();

        return menus;
    }

    @Override
    public List<Menu> getAllMenu() {

        List<Menu> menus = menuMapper.selectList(new QueryWrapper<>());

        if (CollectionUtils.isEmpty(menus)) {
            return null;
        }

        Map<Long, List<Menu>> collect = menus.stream().collect(Collectors.groupingBy(menu -> menu.getParentId()));

        for (Map.Entry<Long, List<Menu>> longListEntry : collect.entrySet()) {

            Long key = longListEntry.getKey();  //父级id
            List<Menu> value = longListEntry.getValue();  //集合数据

            for (Menu menu : menus) {
                if (menu.getMenuId() == key)
                    menu.setChildren(value);
            }

        }

        return collect.get(0L);

    }

    @Override
    public Integer[] getMenuById(Integer id) {

        Integer[] integers = menuMapper.selectList_BY_ROLE_id(id);
//        System.out.println("menus = " + menus);
//
//        if (CollectionUtils.isEmpty(menus)) {
//            return null;
//        }
//
//        Map<Long, List<Menu>> collect = menus.stream().collect(Collectors.groupingBy(menu -> menu.getParentId()));
//
//        for (Map.Entry<Long, List<Menu>> longListEntry : collect.entrySet()) {
//
//            Long key = longListEntry.getKey();  //父级id
//            List<Menu> value = longListEntry.getValue();  //集合数据
//
//            for (Menu menu : menus) {
//                if (menu.getMenuId() == key)
//                    menu.setChildren(value);
//            }
//
//        }

        return integers ;


    }
}
